import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
// 父路由的组件
import Layout from '@/layout'

// 导入动态路由配置
import approvalsRouter from './modules/approvals' // 组织架构
import departmentsRouter from './modules/departments' // 角色管理
import employeesRouter from './modules/employees' // 员工管理
import permissionRouter from './modules/permission' // 权限管理
import attendancesRouter from './modules/attendances' // 审批
import salarysRouter from './modules/salarys' // 考勤
import settingRouter from './modules/setting' // 工资
import socialRouter from './modules/social' // 社保

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes 静态路由（写死的，固定的）| 动态路由（根据用户权限动态添加）
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// 配置路由规则
// 1. 动态路由
export const asyncRoutes = [
  departmentsRouter, // 组织架构
  settingRouter, // 角色管理
  employeesRouter, // 员工管理
  permissionRouter, // 权限管理
  approvalsRouter, // 审批
  attendancesRouter, // 考勤
  salarysRouter, // 工资
  socialRouter // 社保
]

// 2. 静态路由
export const constantRoutes = [
  // 登录页
  {
    path: '/login',
    // 路由懒加载
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true, // false->显示
    meta: {
      title: '404',
      icon: 'dashboard'
    }
  },

  // 员工批量导入
  {
    path: '/import',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/import')
      }
    ]
  },

  // 首页
  {
    path: '/',
    component: Layout, // 父页面
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }

  // 404 页必须放在末尾
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  // 每次切换页面，对页面的滚动条滚动位置重置为0
  scrollBehavior: () => ({ y: 0 }),

  // 路由的配置数组
  // 回顾: 合并对象 -> {...obj1, ...obj2}
  // 合并数组: [...arr1, ...arr2]
  // 说明: 合并静态和动态路由的配置
  // routes: [...constantRoutes, ...asyncRoutes]
  routes: [...constantRoutes]
})
// 创建路由实例
const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
